<?
	require "lib.php";
	
	if(!boardExists($_GET["bid"])) alertAndBack("존재하지 않는 게시판 입니다!");
	$article = getArticle($_GET["bid"], intval($_GET["id"]));
	if($article === false) { alertAndBack("글이 존재하지 않습니다!"); exit; }
	if(!$article["title"] && $_SESSION["loginAdmin"] != "True") { alertAndBack("삭제된 글입니다!"); exit; }
	updateViewcount($_GET["bid"], $_GET["id"]);
	
	$board_info = boardInfo($_GET["bid"]);
	
	if($board_info["readadmin"] == "1" && !$_SESSION["loginAdmin"] && $article["memberid"] != $_SESSION["loginIdx"]) { alertAndBack("권한이 없습니다!"); exit; }
	
	$userinfo = getUserInfo($article["memberid"]);
	$myinfo = getUserInfo($_SESSION["loginIdx"]);
	
	$all_articles = getAllArticle($_GET["bid"]);
	
	$article_count = count($all_articles);
	$page_need = ceil($article_count / $articlePerPage);
	
	$current_page = ($_GET["page"]) ? intval($_GET["page"]) : 1;

	$article_from = $article_count - ($current_page*$articlePerPage) + 1;
	$article_to = $article_count - (($current_page - 1)*$articlePerPage);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
	<title> IWOP BOARD ALPHA </title>
</head>
<body>
	<table width="600px" border="1" style="table-layout:fixed;overflow:scroll;">
		<tr>
			<td width="70px">제목</td>
			<td width="290px"><? echo stripslashes($article["title"]); ?></td>
			<td width="70px">조회수</td>
			<td width="170px"><? echo $article["viewcount"]; ?></td>
		</tr>
		<tr>
			<td>작성자</td>
			<td><? echo stripslashes($userinfo["name"]); ?></td>
			<td>작성일</td>
			<td><? echo $article["writedate"]; ?></td>
		</tr>
		<tr>
			<td>내용</td>
			<td colspan="3" style="overflow:hidden;"><? echo nl2br(strip_tags(stripslashes($article["content"]))); ?></td>
		</tr>
		<?
			$comments = getComment($_GET["bid"], $article["idx"]);
			if(count($comments) != 0) {
				echo "<tr><td colspan=\"4\">댓글</td></tr>";
				foreach($comments as $comment) {
					$commentuserinfo = getUserInfo($comment["memberid"]);
					echo "<tr><td>".$commentuserinfo["name"]."</td><td colspan=\"2\" style=\"overflow:hidden;\">".stripslashes(strip_tags($comment["content"]))."</td><td>".$comment["writedate"]."</td></tr>\n";
				}
			}
		?>
		<? if(checkLogin()) { ?>
	<form action="insertcomment.php?bid=<?echo $_GET["bid"];?>" method="post">
		<? echo "<input type=\"hidden\" name=\"aid\" value=\"".$article["idx"]."\" />\n"; ?>
		<tr>
			<td colspan="4"><input type="text" name="content" size="84" maxlength="95" /> <input type="submit" value="작성" /></td>
		</tr>
	</form>
		<? } ?>
		<tr>
			<td colspan="4">
				<? 
					echo "<a href=\"list.php?bid=".$_GET["bid"]."\">목록</a>";
					if($userinfo["idx"] == $myinfo["idx"] || $_SESSION["loginAdmin"] == "True") 
						echo " | <a href=\"edit.php?id=".$_GET["id"]."&bid=".$_GET["bid"]."\">편집</a> | <a href=\"delete.php?id=".$_GET["id"]."&bid=".$_GET["bid"]."\">삭제</a>\n"; 
				?>
			</td>
		</tr>
	</table>
	
	<br /><br /><br />
	
	
	<table width="600px" border="1">
		<tr>
			<td width="50px">번호</td>
			<td width="150px">이름</td>
			<td width="400px">제목</td>
		</tr>
<?
	foreach($all_articles as $article) {
		$userinfo = getUserInfo($article["memberid"]);
		if(intval($article["idx"]) >= $article_from && intval($article["idx"]) <= $article_to) {
			echo "<tr><td>".$article["idx"]."</td><td>".stripslashes($userinfo["name"])."</td><td>";
			if($board_info["readadmin"] == "1" && !$_SESSION["loginAdmin"] && $article["memberid"] != $_SESSION["loginIdx"]) echo "비밀글입니다.";
			else if($article["title"]) echo "<a href='view.php?id=".$article["idx"]."&bid=".$_GET["bid"]."'>".stripslashes($article["title"])." [".$article["commentcount"]."]</a>";
			else {
				if($_SESSION["loginAdmin"] == "True") echo "<a href='view.php?id=".$article["idx"]."&bid=".$_GET["bid"]."'>삭제된 글 보기</a>";
				else echo "삭제된 글입니다.";
			}
			echo "</td></tr>";
		}
	}
?>
	</table>
<?
	echo "Page : ";
	for($page=1 ; $page<=$page_need ; $page++) {
		if($page == $current_page) echo "<span style='font-weight:bold'>";
		else echo "<a href='list.php?page=".$page."&bid=".$_GET["bid"]."'>";
		echo $page;
		if($page == $current_page) echo "</span> ";
		else echo "</a> ";
	}		
	echo "<br />";
?>
	<a href="write.php?bid=<?echo $_GET["bid"];?>">글 쓰기</a> | 
	<? if(checkLogin()) echo "<a href=\"logout.php\">로그아웃</a> | "; else echo "<a href=\"join.php\">회원가입</a> | "; ?>
	<? if(checkLogin()) echo $_SESSION["loginName"]."님\n"; else echo "<a href=\"login.php\">로그인</a>\n"; ?>
	
	
</body>
</html>